//
//  HQExcempleUploadViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/11.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQExcempleUploadViewController.h"

@interface HQExcempleUploadViewController ()

@property(nonatomic,strong)NSString *imgurl;

@end

@implementation HQExcempleUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的案例上传";
}

/**
 保存数据
 
 @param sender 保存数据
 */
- (IBAction)saveAction:(id)sender {
   if(_updateImg.image == nil){
        [self tostView:@"请上传图片"];
        return;
    }else if([self isInputEmpty:_userName.text]){
        [self tostView:@"请输入客户名称"];
        return;
    }else if([self isInputEmpty:_userIphoneFiele.text]){
        [self tostView:@"请输入联系方式"];
        return;
    }else if([self isInputEmpty:_xiaoquName.text]){
        [self tostView:@"请输入小区名称"];
        return;
    }else if([self isInputEmpty:_UINameTextField.text]){
        [self tostView:@"请输入开单人名称"];
        return;
    }else if([self isInputEmpty:_uiLinian.text]){
        [self tostView:@"请输入设计理念"];
        return;
    }
    [self showHudText:@"上传图片..."];
    HQNetWork *hqnetwork = [[HQNetWork alloc]init];
    [hqnetwork httpUploadImages:_updateImg.image URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
        [self hiddnHud];
        if([suc[kStatus]integerValue] == 1){
            self.imgurl = [[suc objectForKey:@"data"] objectForKey:@"up_imgurl"];
            [self uploadAnli];
        }else{
            [self tostView:suc[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 保存案例数据
 */
-(void)uploadAnli{
    [self showHudText:@"上传数据..."];
    [HQNetWork requetParam:[HQParam upLoadAnliwithUserId:[UserModelManager userID] withDesignerName:_UINameTextField.text withCaseName:_userName.text withCasePhone:_userIphoneFiele.text  withDesignerContent:_uiLinian.text withCaseImg:self.imgurl withPlotName:_xiaoquName.text] withMethod:[HQMethod addCase] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"上传案例成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.delegate reloadMyExcempleData];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择图片或拍照

 @param sender 按钮
 */
- (IBAction)selectImage:(id)sender {
    NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"HQAddPhotoView" owner:nil options:nil];
    HQAddPhotoView *photoView = objs[0];
    photoView.delegate = self;
    photoView.frame = [UIScreen mainScreen].bounds;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:photoView];
}

/**
 拍照上传图片
 */
-(void)paizhao{
    NSLog(@"拍照");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeCamera context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _updateImg.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}

/**
 相册选择图片
 */
-(void)xiangce{
    NSLog(@"照片");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeLocal context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _updateImg.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}

@end
